<?php

namespace app\admin\controller;

use think\exception\ValidateException;
use app\model\Store;
use app\model\Operatingcity;
use app\model\Feedback;

class FeedbackController extends Base
{

	function index()
	{
		$weid = weid();
		$keyword = input('post.keyword', '', 'serach_in');
		$query = Feedback::where(['f.weid' => $weid])->alias('f')->leftJoin('member m', 'm.id = f.uid');


		$query->with(['member']);

		if (!empty($keyword)) {
			$query->where('f.mobile|f.email|f.message|m.nickname|m.mobile', 'like', '%' . $keyword . '%');
		}

		$res = $query->field('f.*')->order('f.id desc')
			->paginate(getpage())
			->toArray();

		$data['data'] = $res;

		return $this->json($data);
	}

	function getInfo()
	{
		$id =  $this->request->post('id', '', 'serach_in');
		if (!$id) throw new ValidateException('参数错误');

		$data = Feedback::where(['id'=>$id])->with('member')->find();
		if (!empty($data)) {
			$data = $data->toArray();
			$data['image'] = json_decode($data['image'], true);
			$data['create_time'] = date('Y-m-d H:i:s', $data['create_time']);
		}

		return $this->json(['data' => $data]);
	}
	function listUpdate()
	{
		$data = only('id,status,sort');
		if (!$data['id']) throw new ValidateException('参数错误');
		Feedback::update($data);

		return $this->json(['msg' => '操作成功']);
	}
	// 处理售后订单
	public function refundhandle()
	{
		$postdata = input('post.');
		try {
			$postdata['OrderRefund']['response_time'] = time();

			$r = OrderRefund::where('id', $postdata['OrderRefund']['id'])->update($postdata['OrderRefund']);
			if ($r && $postdata['OrderRefund']['refund_status'] == 1) {
				$r = Order::refund_order($postdata);
			}
		} catch (\Exception $e) {
			throw new ValidateException($e->getMessage());
		}

		if ($r) {
			return $this->json(['msg' => '处理成功！']);
		} else {
			throw new ValidateException('处理失败');
		}
	}

	function delete()
	{
		$idx =  $this->request->post('id', '', 'serach_in');
		if (!$idx) throw new ValidateException('参数错误');
		Order::destroy(['id' => explode(',', $idx)], true);
		OrderRefund::where(['order_id' => explode(',', $idx)])->delete();
		return $this->json(['msg' => '操作成功']);
	}
}
